include std/math.e

include core.e
include wrap_helpers.e
include std/machine.e
include std/memory.e   --wrong but free fails to resolve

constant
	hQtGetOpenFileName = define_c_func(hEuQt, stdcall&"qt_get_open_filename", {P, P, P, P, I}, P),
	hQtAbout = define_c_proc(hEuQt, stdcall&"qt_about", {P, P, P}),
	hQtAboutQt = define_c_proc(hEuQt, stdcall&"qt_about_qt", {P, P}),
	hQtCritical = define_c_func(hEuQt, stdcall&"qt_critical", {P,P,P,I,I}, I),
	hQtInformation = define_c_func(hEuQt, stdcall&"qt_information", {P, P, P, I, I}, I),
	hQtQuestion = define_c_func(hEuQt, stdcall&"qt_question", {P, P, P, I, I}, I),
	hQtWarning = define_c_func(hEuQt, stdcall&"qt_warning", {P, P, P, I, I}, I),
	hQtFontDialog = define_c_func(hEuQt, stdcall&"qt_FontDialog", {}, P),
	hQtColorDialog = define_c_func(hEuQt, stdcall&"qt_ColorDialog", {}, P),
	$
--****
--<<LEVELTOC depth=2>>

public function get_open_file_dialog(atom parent, sequence caption, sequence dir, sequence filter, integer opts=0)
	atom pCaption = allocate_string(caption,1),
		pDir = allocate_string(dir,1),
		pFilter = allocate_string(filter,1)
	
	atom result = c_func(hQtGetOpenFileName, {parent, pCaption, pDir, pFilter, opts})

	--~ free(pCaption)
	--~ free(pDir)
	--~ free(pFilter)

	return peek_string(result)
end function

public procedure about(atom parent, sequence title, sequence text)
	atom pTitle = allocate_string(title,1),
		pText = allocate_string(text,1)
	c_proc(hQtAbout, {parent, pTitle, pText})
	--~ free(pTitle)
	--~ free(pText)
end procedure

public procedure aboutQt(atom parent, sequence title)
	atom pTitle = allocate_string(title,1)
	c_proc(hQtAboutQt, {parent, pTitle})
	--~ free(pTitle)
end procedure

function message_dialog(integer rid, atom parent, sequence caption, sequence message,
		object buttons = QMessageBox_Ok, integer defaultButton = -1)
	atom pCaption = allocate_string(caption,1),
		pMessage = allocate_string(message,1)

	if sequence(buttons) then
		if defaultButton = -1 then
			defaultButton = buttons[1]
		end if
		buttons = or_all(buttons)
	end if

	if defaultButton = -1 then
		defaultButton = QMessageBox_Ok
	end if

	integer result = c_func(rid, {parent, pCaption, pMessage, buttons, defaultButton})

	--~ free(pCaption)
	--~ free(pMessage)

	return result
end function

public function critical(atom parent, sequence caption, sequence message,
		object buttons = QMessageBox_Ok, integer defaultButton = -1)
	return message_dialog(hQtCritical, parent, caption, message, buttons, defaultButton)
end function

public function information(atom parent, sequence caption, sequence message,
		object buttons = QMessageBox_Ok, integer defaultButton = -1)
	return message_dialog(hQtInformation, parent, caption, message, buttons, defaultButton)
end function

public function question(atom parent, sequence caption, sequence message,
		object buttons = QMessageBox_Ok, integer defaultButton = -1)
	return message_dialog(hQtQuestion, parent, caption, message, buttons, defaultButton)
end function

public function warning(atom parent, sequence caption, sequence message,
		object buttons = QMessageBox_Ok, integer defaultButton = -1)
	return message_dialog(hQtWarning, parent, caption, message, buttons, defaultButton)
end function

public function FontDialog()
	return c_func(hQtFontDialog, {})
end function

public function ColorDialog()
	return c_func(hQtColorDialog, {})
end function




